const puppeteer = require('puppeteer')

  var url = `https://www.biquge.cc/html/48/48839/3741851.html`
async function spider  (cb) {
  const browser = await puppeteer.launch({
    hadeless: false,
    executablePath: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
  })
  const page = await browser.newPage();
  let number = 0
  while(number<100){
    number++
    console.log(1)
    await page.goto(url,{waitUntil:'networkidle2'});//页面进入目标地址，后一参数表示等待页面加载完毕
    console.log(2)
    let t = await page.evaluate(()=>{
      var charper ={
        title: '',
        content: '',
        nextUrl: ''
      }
      let $ = window.$;//获取jQuery
      // console.log($('.bookname h1').text())
      charper.title = $('.bookname h1').text()
      charper.content = $('#content').html()
      if($('#A3')){
        charper.nextUrl = $('#A3').attr('href')
      }
      let str = '<script>chaptererror();</script><div align="center"><a href="javascript:postError();" style="text-align:center;color:red;">章节错误,点此举报(免注册)</a>,举报后维护人员会在两分钟内校正章节内容,请耐心等待,并刷新页面。</div>'
      charper.content = charper.content.replace(str,'')
      charper.content = charper.content.replace(/<br>\s*<br>/g,'\r\n')
      return charper
    })
    url =  `https://www.biquge.cc/html/48/48839/${t.nextUrl}.html`
    if(cb){
      cb(t)
    } 
    }
  }

module.exports = spider